<template>
	<el-dialog :title="dialogTitle" :visible.sync="visible" width="640px" top="0" append-to-body>
		<el-form v-bind="formConfig" :model="formData" :rules="formRules" label-width="108px">
			<el-row :gutter="16">
				<el-col :span="24">
					<el-form-item label="数据适用类型" prop="scopeType">
						<el-radio-group v-model="formData.scopeType" v-removeAriaHidden>
							<el-radio label="1">网站基本信息</el-radio>
							<el-radio label="2">默认图片</el-radio>
							<el-radio label="3">隐藏</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="基本信息名称" prop="label">
						<el-input v-model="formData.label"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="基本信息类型" prop="type">
						<el-radio-group v-model="formData.type" v-removeAriaHidden>
							<el-radio label="1">单行文本</el-radio>
							<el-radio label="2">图片上传</el-radio>
							<el-radio label="3">多行文本</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="基本信息编码" prop="code">
						<sw-letter-input v-model="formData.code" placeholder="请输入编码"></sw-letter-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="排序">
						<el-input-number v-model="formData.orderBy" controls-position="right" :min="1" style="width: 100%" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template slot="footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="handleSubmitForm()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { addedBaseInfo, updateBaseInfo } from '@/api/system/sysBaseInfo';
export default {
	props: {
		value: { type: Boolean, default: false },
		data: { type: Object, default: () => {} }
	},
	data() {
		return {
			visible: this.value,
			formName: '基本信息',
			formData: {},
			formRules: {
				code: [{ required: true, message: '请输入基本信息编码', trigger: 'blur' }],
				label: [{ required: true, message: '请输入基本信息名称', trigger: 'blur' }],
				type: [{ required: true, message: '请选择基本信息类型', trigger: 'change' }],
				scopeType: [{ required: true, message: '请选择数据适用类型', trigger: 'change' }]
			},
			formConfig: { ref: 'form', size: 'medium', class: 'dialog_form_wrapper' }
		};
	},
	computed: {
		addOrEdit() {
			return Object.keys(this.data).length === 0;
		},
		callFunction() {
			return eval(this.addOrEdit ? addedBaseInfo : updateBaseInfo);
		},
		dialogTitle() {
			return `${this.addOrEdit ? '新增' : '修改'}${this.formName}`;
		}
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			this.$nextTick(() => {
				this.$refs['form'].resetFields();
				this.formData = { ...this.data };
			});
		}
	},
	methods: {
		async handleSubmitForm() {
			let result = await this.callFunction(this.formData).then((res) => res.data);
			if (result.code === '0') {
				this.$message.success(`${this.dialogTitle}成功`);
				this.$emit('refresh');
				this.visible = false;
			} else {
				this.$message.error(`${this.dialogTitle}失败`);
			}
		}
	}
};
</script>
